﻿//-----------------------------------------------------------------------
// <copyright file="PivotTableCategoryCollection.cs" company="Forebrain Technology, LLC">
//
// Copyright (c) Forebrain Technology, LLC. All rights reserved.
// 
// This source code is released under the Microsoft Public License (Ms-PL).
//
// </copyright>
//-----------------------------------------------------------------------
namespace PivotCollectionTools.Data
{
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;

    /// <summary>
    /// Collection of Pivot Table Catgory objects.
    /// </summary>
    [Serializable]
    public class PivotTableCategoryCollection : Collection<PivotTableCategory>
    {
        /// <summary>
        /// Gets the <see cref="PivotCollectionTools.Xml.FacetCategory"/> with the specified category name.
        /// </summary>
        /// <param name="categoryName">The name of the catgory to get.</param>
        public PivotTableCategory this[string categoryName]
        {
            get { return this.Items.FirstOrDefault(c => c.Name == categoryName); }
        }

        /// <summary>
        /// Determines whether category collection contains the specified category name.
        /// </summary>
        /// <param name="categoryName">Name of the category.</param>
        /// <returns>
        /// <c>true</c> if the category collection contains the specified category 
        /// name; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(string categoryName)
        {
            return this.Items.FirstOrDefault(c => c.Name == categoryName) != null;
        }

        /// <summary>
        /// Removes the element at the specified index of the <see cref="T:System.Collections.ObjectModel.Collection`1"/>.
        /// </summary>
        /// <param name="index">The zero-based index of the element to remove.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        ///     <paramref name="index"/> is less than zero.
        /// -or-
        ///     <paramref name="index"/> is equal to or greater than <see cref="P:System.Collections.ObjectModel.Collection`1.Count"/>.
        /// -or-
        ///     <paramref name="index"/> is equal to zero which is the Item Attribute category.
        /// </exception>
        protected override void RemoveItem(int index)
        {
            if (index == 0)
            {
                throw new ArgumentOutOfRangeException("index", "Cannot remove Item Attribute category");
            }

            base.RemoveItem(index);
        }        
    }
}